﻿// CanWheel.cpp: 实现文件
//

#include "pch.h"
#include "LiWi.h"
#include "afxdialogex.h"
#include "CanWheel.h"
#include "math.h"	
#include "liwishell.h"
#include "Data.h"

#define _pi		3.1415926


// CanWheel 对话框

IMPLEMENT_DYNAMIC(CanWheel, CDialogEx)

CanWheel::CanWheel(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_CamWheel, pParent)
{

}

CanWheel::~CanWheel()
{
}

void CanWheel::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_CamList, m_CamList);
}


BEGIN_MESSAGE_MAP(CanWheel, CDialogEx)
	ON_BN_CLICKED(IDC_Run, &CanWheel::OnBnClickedRun)
END_MESSAGE_MAP()


// CanWheel 消息处理程序


BOOL CanWheel::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	//创建表格
	CRect rect;
	m_CamList.GetClientRect(&rect);
	m_CamList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);	//整行选择，网格线
	m_CamList.InsertColumn(0, _T("序号"), LVCFMT_LEFT, rect.Width() / 4);
	m_CamList.InsertColumn(1, _T("点位"), LVCFMT_LEFT, rect.Width() * 3 / 4);
	//for (int i = 0; i < 4; i++)
	//{
	//	for (int j = 0; j < 256; j++)
	//	{
	//		CString strNum,strPoint;
	//		strNum.Format(_T("%d"), j + i * 256);
	//		strPoint.Format(_T("%.4f"), (sin(((2 * _pi) / 34) * j)) * 100);

	//		CString str;
	//		str.Format(_T("%d"), m_CamList.GetItemCount());
	//		int nIndex = m_CamList.InsertItem(m_CamList.GetItemCount(), str);
	//		m_CamList.SetItemText(nIndex, 0, strNum);
	//		m_CamList.SetItemText(nIndex, 1, strPoint);
	//	}
	//}

	for (int i = 0; i < 361; i++)
	{
		CString strNum, strPoint;
		strNum.Format(_T("%d"), i);
		strPoint.Format(_T("%.4f"), (i * 25 + 1000 * (1 - cos((i * _pi / 180)))));

		CString str;
		str.Format(_T("%d"), m_CamList.GetItemCount());
		int nIndex = m_CamList.InsertItem(m_CamList.GetItemCount(), str);
		m_CamList.SetItemText(nIndex, 0, strNum);
		m_CamList.SetItemText(nIndex, 1, strPoint);
		setTableValue[i] = _ttof(strPoint);
		
	}
	
	// 
	//初始化值
	for (int i = 0; i < 4; i++)
	{
		CString strAxis;
		strAxis.Format(_T("%d"), i);
		((CComboBox*)GetDlgItem(IDC_FirstAxis))->AddString(strAxis);
		((CComboBox*)GetDlgItem(IDC_SecendAxis))->AddString(strAxis);
	}
	((CComboBox*)GetDlgItem(IDC_FirstAxis))->SetCurSel(0);
	((CComboBox*)GetDlgItem(IDC_SecendAxis))->SetCurSel(1);
	GetDlgItem(IDC_MoveSp)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_MoveLine)->SetWindowTextW(_T("1000"));
	GetDlgItem(IDC_Units_6)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Accel_6)->SetWindowTextW(_T("1000"));
	GetDlgItem(IDC_Decel_6)->SetWindowTextW(_T("1000"));

	return TRUE;  // return TRUE unless you set the focus to a control
	 //异常: OCX 属性页应返回 FALSE
}


void CanWheel::OnBnClickedRun()
{
	// TODO: 在此添加控件通知处理程序代码

	CString axisList[2];
	CString units, speed, accel, decel,moveLine;
	int enable = -1;
	GetDlgItem(IDC_FirstAxis)->GetWindowTextW(axisList[0]);
	GetDlgItem(IDC_SecendAxis)->GetWindowTextW(axisList[1]);
	GetDlgItem(IDC_MoveSp)->GetWindowTextW(speed);
	GetDlgItem(IDC_Units_6)->GetWindowTextW(units);
	GetDlgItem(IDC_Accel_6)->GetWindowTextW(accel);
	GetDlgItem(IDC_Decel_6)->GetWindowTextW(decel);
	GetDlgItem(IDC_MoveLine)->GetWindowTextW(moveLine);
	
	for (int i = 0; i < 361; i++)
	{
		PMC_SetTable(Data::handle, i, 1, &setTableValue[i]);
	}
	

	for (int i = 0; i < 2; i++)
	{
		PMC_GetAxisEnable(Data::handle, _ttoi(axisList[i]), &enable);
		if (enable==0)
		{
			MessageBox(_T("请对轴使能！！！"));
			return;
		}
		PMC_SetUnits(Data::handle, _ttoi(axisList[i]), _ttof(units));
		PMC_SetSpeed(Data::handle, _ttoi(axisList[i]), _ttof(speed));
		PMC_SetAccel(Data::handle, _ttoi(axisList[i]), _ttof(accel));
		PMC_SetDecel(Data::handle, _ttoi(axisList[i]), _ttof(decel));
	}

	
	//启动凸轮链接，凸轮表放在table0-360中，主轴走dis变量的距离跟随轴就运行一遍table0-360中的凸轮数据
	PMC_Cambox(Data::handle, _ttoi(axisList[1]), tableStartPoint, tableEndPoint,_ttof( units), _ttof(moveLine), _ttoi(axisList[0]), 6, 0);
	PMC_Single_Move(Data::handle, _ttoi(axisList[0]), _ttof(moveLine));


}
